// 添加补课成员搜索助手 // 查询方法：orderListSecond;入参listType 3
<template>
  <el-dialog
    v-model="dialogVisible"
    title="添加补课学员"
    width="80%"
    :before-close="handleDialogClose"
  >
    <div class="content">
      <MemberMakeupList
        ref="makeuplistRef"
        v-bind="$props"
        @setItemsValues="setItemsValues"
      />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <div style="float: left">已选择：{{ items.length }}人</div>
        <el-button @click="handleDialogClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import MemberMakeupList from "./memberMakeupList.vue";
import { addTempMemberAPI } from "@/api/registrar/class";

const props = defineProps({
  classId: {
    type: String,
  },
  data: {
    type: Object,
    default: () => {
      return {
        orderNo: null,
        cofId: null,
        courseId: null,
        classDate: null,
      };
    },
  },
});

// 变量--------------------------------------------------------------------------------------------------------------------
const { proxy } = getCurrentInstance();
const makeuplistRef = ref(null);
const emits = defineEmits(["refreshPage"]);
const loading = ref(false);
const dialogVisible = ref(false);
const items = ref([]);

function openSelector() {
  dialogVisible.value = true;
  nextTick(function () {
    makeuplistRef.value.initPage();
  });
}

function handleDialogClose() {
  dialogVisible.value = false;
}

function setItemsValues(value) {
  items.value = value;
}

// 添加成员
function handleSubmit() {
  loading.value = true;
  let arr = [];
  let studentIdArr = [];
  items.value.forEach((i) => {
    i.consumeCourseId = i.courseId;
    i.classId = props.classId;
    arr.push({ id: i.studentId, orderNo: i.orderNo, courseId: i.courseId });
    studentIdArr.push(i.studentId);
  });

  addTempMemberAPI({
    ids: arr,
    cofId: props.data.cofId,
    orderNo: props.data.orderNo,
    classDate: props.data.classDate,
    temp: 2, // 0临时学员1正常学员 2补课学员
  }).then((response) => {
    loading.value = false;
    dialogVisible.value = false;
    refreshParent(studentIdArr);
  });
}

function refreshParent(studentIdArr) {
  emits("refreshPage", studentIdArr);
}

defineExpose({ openSelector }); // 用于父组件调用
</script>
